
class ImagevenuePlugin:
	def __init__(self):
		# put all the links this plugins supports here - it will be used to filter supported URLs
		self.supported_sites = ['imagevenue.com']
		self.supported_urls = self.supported_sites
		self.cookies_to_block = []
		# whether we support pause/resume
		self.supports_resume = False
		# maximum number of concurrent downloads
		self.max_concurrent = 1
		# parameter names we require
		self.param_names = []
		# parameter values
		self.param_vals = {}
		self.name = self.supported_sites[0]

	def get(self, url):
		"""Get a single file from imagevenue"""
		parsed_url = urlparse.urlparse(url)
		src_pat = 'SRC="'
		remote_file = None
		# Find the line with our file location
		data = get_url_data(url)
		for line in data.splitlines():
			if src_pat in line:
				remote_file = '/' + line.split(src_pat)[1].split('"')[0]
				break
			elif 'This image does not exist on this server' in line:
				config.report.report_warning('Image does not exist at URL "%s"' % url)
				return

		if not remote_file:
			raise TemporarilyUnavailable('Error occured while trying to download')

		local_file = self._cleanup_filename(find_attr(parsed_url[4], 'image'))
		download_file_with_retries(local_file, urlparse.urljoin(url, remote_file))
		config.report.report_finish(local_file)

	def url_matches(self, url):
		"""Check whether we support this URL"""
		parsed_url = urlparse.urlparse(url)
		if parsed_url[4] == '': # image is a thumbnail
			return False
		for l in self.supported_urls:
			if l in url:
				return True
		return False

	def version(self):
		"""Return plugin version - when interface changes, the version changes"""
		return 2

	def _cleanup_filename(self, fname):
		"""Clean up filenames from imagevenue"""
		parts = fname.split('_')
		final_name = parts[1]
		for part in parts[2:-2]:
			final_name = final_name + '_' + part
		return final_name + ".jpg"

modules = [ImagevenuePlugin()]
